﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_ICM_PurchaseOrder : IDAL_ICM_PurchaseOrder
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();

        public D_ICM_PurchaseOrder()
        {

        }

        public IList<M_ICM_PurchaseOrder> GetFull(Nullable<int> bgm_year)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_ICM_PurchaseOrder> infoList = new List<M_ICM_PurchaseOrder>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_year", bgm_year);

                strSQL = "SELECT po.PoId, po.BidderId, po.Gfmis, po.Code"
                    + ", po.Name, po.Descr, po.CanInstallment, po.PrepayAmount"
                    + ", po.RedemptionPercent, po.BgYear, po.RecordStatus"
                    + " FROM IcmPurchaseOrder po"
                    + " WHERE po.RecordStatus not like @delete_status"
                    + " AND po.BgYear = @bgm_year";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ICM_PurchaseOrder info = new M_ICM_PurchaseOrder();
                        info.icm_po_seq = Convert.ToInt32(drc[i]["PoId"]);
                        info.icm_bidder_seq = cn.ConvertNullInt(drc[i]["BidderId"]);
                        info.icm_po_gfmis = drc[i]["Gfmis"].ToString();
                        info.icm_po_ref_no = drc[i]["Code"].ToString();
                        info.icm_po_name = drc[i]["Name"].ToString();
                        info.icm_po_descr = drc[i]["Descr"].ToString();
                        info.can_installment = drc[i]["CanInstallment"].ToString();
                        info.prepay_amount = cn.ConvertNullDouble(drc[i]["PrepayAmount"]);
                        info.redemption_percent = cn.ConvertNullDouble(drc[i]["RedemptionPercent"]);
                        info.bgm_year = cn.ConvertNullInt(drc[i]["BgYear"]);

                        info.record_status = drc[i]["RecordStatus"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_ICM_PurchaseOrder> GetList(Nullable<int> bgm_year, string filter)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_ICM_PurchaseOrder> infoList = new List<M_ICM_PurchaseOrder>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_year", bgm_year);
                dbManager.AddParameters(2, "@filter", "%" + filter + "%");

                strSQL = "SELECT *"
                    + " FROM (SELECT po.PoId"
                        + ", concat(po.Code, ' - ', po.Name) as icm_po_name"
                        + " FROM IcmPurchaseOrder po"
                        + " WHERE po.RecordStatus not like @delete_status"
                        + " AND po.BgYear = @bgm_year"
                        + ") t"
                    + (filter.Length > 0 ? " WHERE icm_po_name like @filter" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_ICM_PurchaseOrder info = new M_ICM_PurchaseOrder();
                        info.icm_po_seq = Convert.ToInt32(drc[i]["PoId"]);
                        info.icm_po_name = drc[i]["icm_po_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_ICM_PurchaseOrder GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_ICM_PurchaseOrder info = new M_ICM_PurchaseOrder();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT po.PoId, po.BidderId, po.Gfmis, po.Code"
                    + ", po.Name, po.Descr, po.CanInstallment, po.PrepayAmount"
                    + ", con.ConId, con.NameTh"
                    + ", po.RedemptionPercent, po.BgYear, po.RecordStatus"
                    + " FROM IcmPurchaseOrder po"
                    + " LEFT JOIN IcmBidder bidder ON po.BidderId = bidder.BidderId"
                    + " LEFT JOIN IcmMasContact con ON bidder.ConId = con.ConId"
                    + " WHERE po.PoId = @seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.icm_po_seq = Convert.ToInt32(drc[0]["PoId"]);
                    info.icm_bidder_seq = cn.ConvertNullInt(drc[0]["BidderId"]);
                    info.icm_po_gfmis = drc[0]["Gfmis"].ToString();
                    info.icm_po_ref_no = drc[0]["Code"].ToString();
                    info.icm_po_name = drc[0]["Name"].ToString();
                    info.icm_po_descr = drc[0]["Descr"].ToString();
                    info.can_installment = drc[0]["CanInstallment"].ToString();
                    info.prepay_amount = cn.ConvertNullDouble(drc[0]["PrepayAmount"]);
                    info.redemption_percent = cn.ConvertNullDouble(drc[0]["RedemptionPercent"]);
                    info.bgm_year = cn.ConvertNullInt(drc[0]["BgYear"]);
                    info.icm_con_seq = cn.ConvertNullInt(drc[0]["ConId"]);
                    info.icm_con_name = drc[0]["NameTh"].ToString();

                    info.record_status = drc[0]["RecordStatus"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
    }
}
